<?php

class Default_Models_Vote extends Libs_Model {

    public function __construct() {
        parent::__construct();
    }

    public function insertThumbUp($id) {
        $ip = $this->db->getUserIp();
        $sql = "select ip from tbl_vote where pro_id = $id and ip = '$ip'";
        $result = $this->db->fetchAllO($sql);
        $count = mysql_num_rows($result);

        if ($count == 0) {
            $sql = "update tbl_products set thumb_up = thumb_up + 1 where pro_id = $id";
            $this->db->query($sql);

            $sql = "insert into tbl_vote(pro_id, ip) values($id, '$ip')";
            $this->db->query($sql);


//            $sql = "select thumb_up from tbl_products where pro_id = $id";
//            $result = $this->db->query($sql);
//            $row = mysql_fetch_array($result);
//            $up = $row['thumb_up'];
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function getThumbUp($id) {
        $sql = "select thumb_up from tbl_products where pro_id = $id";
        $result = $this->db->query($sql);
        $row = mysql_fetch_array($result);
        $up = $row['thumb_up'];
        return $up;
    }
    
    
    public function insertThumbDown($id) {
        $ip = $this->db->getUserIp();
        $sql = "select ip from tbl_vote where pro_id = $id and ip = '$ip'";
        $result = $this->db->fetchAllO($sql);
        $count = mysql_num_rows($result);

        if ($count == 0) {
            $sql = "update tbl_products set thumb_down = thumb_down + 1 where pro_id = $id";
            $this->db->query($sql);

            $sql = "insert into tbl_vote(pro_id, ip) values($id, '$ip')";
            $this->db->query($sql);
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function getThumbDown($id) {
        $sql = "select thumb_down from tbl_products where pro_id = $id";
        $result = $this->db->query($sql);
        $row = mysql_fetch_array($result);
        $down = $row['thumb_down'];
        return $down;
    }

}
